package solutions;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 黎鹤舞
 * Date: 2023-12-24
 * Time: 18:44
 */
class MinStack {

    Stack<Integer> stack;
    Stack<Integer> minStack;

    //构造函数
    public MinStack() {
        this.stack = new Stack<>();
        this.minStack = new Stack<>();
    }

    //压栈、入栈
    public void push(int val) {
        //如果stack为空
        if(this.stack.empty()) {
            stack.push(val);
            minStack.push(val);
        }else {
            if(val <= minStack.peek()) {
                minStack.push(val);
            }
            stack.push(val);
        }

    }


    //出栈
    public void pop() {
        //如果为空
        if(this.stack.empty()) {
            return;
        }
        //这里不能写作==
//        if(stack.peek() == minStack.peek()) {
//            minStack.pop();
//        }
        if(stack.pop().equals(minStack.peek())) {
            minStack.pop();
        }
        stack.pop();

    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }
}
